/****************************************Copyright (c)****************************************************
**                               China Mobile M2M Co., LTD
**
**                                 http://iot.10086.cn/
**
**--------------File Info---------------------------------------------------------------------------------
** File name:           app_gpio.c
** Created by:          yangpengbo                  ID:03900265
** Created date:        20161109
** Version:             V1.00
** Descriptions:        creat file
**
**--------------------------------------------------------------------------------------------------------
** Modified by:         x
** Modified date:       x
** Version:             x
** Descriptions:        x
**
** Rechecked by:
*********************************************************************************************************/
#include "include.h"
/*********************************************************************************************************
**                                          MACRO DEF
*********************************************************************************************************/

/*********************************************************************************************************
**                                          ENUM&STRUCT
*********************************************************************************************************/

/*********************************************************************************************************
**                                          GLOBAL VAR
*********************************************************************************************************/

/*********************************************************************************************************
**                                          LOCAL VAR
*********************************************************************************************************/
static GpioStatusStruct  s_gpio = {0};

static  U32     g_red_led_curtime = 0;
static  U32     g_blue_led_curtime = 0;
static  U32     g_green_led_curtime = 0;

static  U32		g_voltage_value = 0;
/*********************************************************************************************************
** Function name:       Gpio_VarInit
** Descriptions:        Gpio_VarInit
** input parameters:    none
** output parameters:   none
** Returned value:      none
*********************************************************************************************************/
void    Gpio_VarInit(void)
{
    s_gpio.alarm_switch = TRUE;

    s_gpio.accoff_toalarm_delayconst = 20;
}

/*********************************************************************************************************
** Function name:       Led_ModuleHandle
** Descriptions:        Led_ModuleHandle 100ms
** input parameters:    none
** output parameters:   none
** Returned value:      none
*********************************************************************************************************/
void    Led_ModuleHandle(void)
{
    //red, cank
    //green, gsm
    //blue, gps
    static U32  led_handle_tick = 0;

    if(Device_GetWorkMode() == TEST_MODE)
    {
        return;
    }

	if(TRUE == device_getSleepBit())
    {
        Gpio_LedOff(LED_ALL);
        return;
    }

    led_handle_tick++;

    //can k led handle
    if(TRUE == Device_CanKLineDataAvailable())
    {
        Gpio_LedOn(LED_CANK);
        g_red_led_curtime = led_handle_tick;
    }
    else
    {
        if((led_handle_tick - g_red_led_curtime) >= LED_1HZ_BLINK_TIME)
        {
            Gpio_LedToggle(LED_CANK);
            g_red_led_curtime = led_handle_tick;
        }
    }

    //gps led handle
    if((FALSE == gps_data1Available())
       && (gps_isRefLocSyncOk() == FALSE)
       && ((led_handle_tick - g_blue_led_curtime) >= LED_1HZ_BLINK_TIME))
    {
        Gpio_LedToggle(LED_GPS);
        g_blue_led_curtime = led_handle_tick;
    }
    if((FALSE == gps_data1Available())
       && (gps_isRefLocSyncOk() == TRUE)
       && ((led_handle_tick - g_blue_led_curtime) >= LED_2HZ_BLINK_TIME))
    {
        Gpio_LedToggle(LED_GPS);
        g_blue_led_curtime = led_handle_tick;
    }
    if(TRUE == gps_data1Available())
    {
        Gpio_LedOn(LED_GPS);
        g_blue_led_curtime = led_handle_tick;
    }

    //gsm led handle
    if((FALSE == gsm_isCregOk())
       && ((led_handle_tick - g_green_led_curtime) >= LED_1HZ_BLINK_TIME))
    {
        Gpio_LedToggle(LED_GREEN);
        g_green_led_curtime = led_handle_tick;
    }
    if((TRUE == gsm_isCregOk())
       && (FALSE == Server_GetLoginFlag())
       && ((led_handle_tick - g_green_led_curtime) >= LED_2HZ_BLINK_TIME))
    {
        Gpio_LedToggle(LED_GREEN);
        g_green_led_curtime = led_handle_tick;
    }
    if(TRUE == Server_GetLoginFlag())
    {
        Gpio_LedOn(LED_GREEN);
        g_green_led_curtime = led_handle_tick;
    }


}
/*********************************************************************************************************
** Function name:       Acc_ModuleHandle
** Descriptions:        Acc_ModuleHandle 100ms
** input parameters:    none
** output parameters:   none
** Returned value:      none
*********************************************************************************************************/
void    Acc_ModuleHandle(void)
{
    //acc on, clr alarm bit (maybe can k on)
    //acc off, set alarm bit (maybe can k off)
    if(Device_GetWorkMode() == TEST_MODE)
        return;

    if(TRUE == Gpio_ReadAccPin())
    {
        if(s_gpio.acc_filter < GPIO_ACC_FILTER_CONST)
        {
            s_gpio.acc_filter++;
            return;
        }

        if(s_gpio.acc_status == FALSE)
        {
            s_gpio.acc_status = TRUE;
            if(s_gpio.alarm_switch == TRUE)
            {
                device_setAlarmBit(FALSE);
                iot_logInfo("clr alarm bit");

                //save nv, send vdtp
            }
        }

    }
    else
    {
        if(s_gpio.acc_filter > 0)
        {
            s_gpio.acc_filter--;
            return;
        }

        if(s_gpio.acc_status == TRUE)
        {
            s_gpio.acc_status = FALSE;
            s_gpio.accoff_toalarm_delaytime = com_sysTimeRead();
        }

        if(((com_sysTimeRead() - s_gpio.accoff_toalarm_delaytime) >= s_gpio.accoff_toalarm_delayconst)
           && (s_gpio.accoff_toalarm_delaytime != MAX_CNT_CONST))
        {
            if(s_gpio.alarm_switch)
            {
                device_setAlarmBit(TRUE);
                iot_logInfo("set alarm bit");
                //save nv, send vdtp
            }
            s_gpio.accoff_toalarm_delaytime = MAX_CNT_CONST;
        }
    }
}
/*********************************************************************************************************
** Function name:       Acc_GetStatus
** Descriptions:        Acc_GetStatus
** input parameters:    none
** output parameters:   none
** Returned value:      TRUE or FALSE
*********************************************************************************************************/
BOOL    Acc_GetStatus(void)
{
    return s_gpio.acc_status;
}
/*********************************************************************************************************
** Function name:       Vol_ModuleHandle
** Descriptions:        Vol_ModuleHandle    100ms
** input parameters:    none
** output parameters:   none
** Returned value:      none
*********************************************************************************************************/
void    Vol_ModuleHandle(void)
{
    static U16 batteryvol[VOLTAGE_DETECT_TICK] = {0};
    static U8 voltagedetecttick = 0;
    static U16 prevalue = 0;
    float f_tmpvalue = 0;
    static float f_prevalue = 0;
    static U32 calctemp = 0;
    static U32 voltage_handle_time = 0;
    U8 i = 0;
    U32 tempvalue;
    U16 tmpvalue = 0;

	if(Device_GetWorkMode() == TEST_MODE)
        return;
	
    if(com_sysTimeRead() == voltage_handle_time)
        return;
    else
        voltage_handle_time = com_sysTimeRead();

    if(0 == prevalue)
    {
        iot_logDebug("ADC: %d", iot_readAdc(VOL_ADC_CHAN));

        if(voltagedetecttick < VOLTAGE_DETECT_TICK)
        {
            tmpvalue = iot_readAdc(VOL_ADC_CHAN);
            batteryvol[voltagedetecttick] = tmpvalue;
            voltagedetecttick++;
        }
        else
        {
            voltagedetecttick = 0;
            prevalue = 0;
            for (i = 0; i < VOLTAGE_DETECT_TICK; i++)
            {
                prevalue += batteryvol[i];
            }
            prevalue /= VOLTAGE_DETECT_TICK;

            f_prevalue = prevalue;
            iot_logInfo("ADC f_prevalue : %d", (U32)(f_prevalue*3919.27/100+300));
        }
        return;
    }

    //step1: get adc voltage
    if(voltagedetecttick < VOLTAGE_DETECT_TICK)
    {

        f_tmpvalue = f_prevalue + (iot_readAdc(VOL_ADC_CHAN) - f_prevalue)/10;
        f_prevalue = f_tmpvalue;

        batteryvol[voltagedetecttick] = (U16)f_tmpvalue;
        voltagedetecttick++;
    }
    else
    {
        voltagedetecttick = 0;
        //step 1.1: get 3 times value  and get the average value
        calctemp = 0;
        for (i = 0; i < VOLTAGE_DETECT_TICK; i++)
        {
            calctemp += batteryvol[i];
        }
        calctemp /= VOLTAGE_DETECT_TICK;

        tempvalue = calctemp*3919.27/100+300;;

		g_voltage_value = tempvalue;
    }

}
/*********************************************************************************************************
** Function name:       Vol_GetValue
** Descriptions:        Vol_GetValue 
** input parameters:    none
** output parameters:   none
** Returned value:      none
*********************************************************************************************************/
U32 	Vol_GetValue(void)
{
	return g_voltage_value;
}

/*********************************************************************************************************
End Of File
*********************************************************************************************************/


